<?php

/**
 * This is the model class for table "{{adm_privilege}}".
 *
 * The followings are the available columns in table '{{adm_privilege}}':
 * @property integer $zap_id
 * @property integer $zap_adm_id
 * @property integer $zap_type
 * @property string $parent
 * @property string $child
 * @property integer $inputtime
 * @property integer $updatetime
 */
class AdmPrivilege extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdmPrivilege the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adm_privilege}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zap_adm_id, zap_type, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('parent, child', 'length', 'max'=>64),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zap_id, zap_adm_id, zap_type, parent, child, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'action'=>array(self::HAS_ONE, 'AdmGroup', array('name'=>'child'),'select'=>'zag_title,name'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zap_id' => 'Zap',
			'zap_adm_id' => 'Zap Adm',
			'zap_type' => 'Zap Type',
			'parent' => 'Parent',
			'child' => 'Child',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zap_id',$this->zap_id);
		$criteria->compare('zap_adm_id',$this->zap_adm_id);
		$criteria->compare('zap_type',$this->zap_type);
		$criteria->compare('parent',$this->parent,true);
		$criteria->compare('child',$this->child,true);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}else{
			$this->updatetime = time();
		}
		return true;
	}
	
	public function DelCatcache(){
		Yii::app()->filecache->delete('adm_role_action');
	}
	
	function afterSave(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}
	
	function afterDelete(){
		//清空缓存
		$this->DelCatcache();
		return true;
	}
}